<?php
class ModelProductProduct extends Model {
	public function addProduct($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "product SET category_id = '" . (int)$data['category_id'] . 
			"', manufacturer_id = '" . (int)$data['manufacturer_id'] . 
			"', `name` = '" . $this->db->escape($data['name']) . 
			"', ean = '" . $this->db->escape($data['ean']) . 
			"', model = '" . $this->db->escape($data['model']) . 
			"', image = '" .  $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . 
			"', price = '" . (float)$data['price'] . 
			"', weight = '" . (float)$data['weight'] . 
			"', quantity = '" . (int)$data['quantity'] . 
			"', minimum = '" . (int)$data['minimum'] . 
			"', processing_day = '" . (int)$data['processing_day'] . 
			"', layout_id = '" . (int)$data['layout_id'] . 
			"', sort_order = '" . (int)$data['sort_order'] . 
			"', status = '" . (int)$data['status'] . 
			"', date_added = UNIX_TIMESTAMP(), date_modified = UNIX_TIMESTAMP()");
		$product_id = $this->db->getLastId();
		
		foreach ($data['product_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '" . $this->db->escape($value['display_name']) . 
				"', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . 
				"', meta_description = '" . $this->db->escape($value['meta_description']) . 
				"', tag = '" . $this->db->escape($value['tag']) . 
				"', description = '" . $this->db->escape($value['description']) . "'");
		}
		
		if (isset($data['product_image'])) {
			foreach ($data['product_image'] as $product_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . 
					"', image = '" . $this->db->escape(html_entity_decode($product_image['image'], ENT_QUOTES, 'UTF-8')) . 
					"', sort_order = '" . (int)$product_image['sort_order'] . "'");
			}
		}

		if (isset($data['product_attribute'])) {
			foreach ($data['product_attribute'] as $product_attribute) {
				foreach($product_attribute['product_attribute_value'] as $product_attribute_value){
					$this->db->query("INSERT INTO " . DB_PREFIX . "product_attribute SET product_id = '" . (int)$product_id . 
						"', attribute_id = '" . (int)$product_attribute['attribute_id'] . 
						"', attribute_value_id = '" . (int)$product_attribute_value['attribute_value_id'] . 
						"', attr_sort = '" . (int)$product_attribute['attr_sort'] . 
						"', value_sort = '" . (int)$product_attribute_value['value_sort'] .  "'");
				}
			}
		}
	
		if (isset($data['product_option'])) {
			foreach ($data['product_option'] as $product_option) {
				foreach($product_option['product_option_value'] as $product_option_value){
					$this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . 
						"', option_id = '" . (int)$product_option['option_id'] . 
						"', option_value_id = '" . (int)$product_option_value['option_value_id'] . 
						"', required = '" . (int)$product_option['required'] . 
						"', quantity = '" . (int)$product_option_value['quantity'] . 
						"', price = '" . (float)$product_option_value['price'] . 
						"', weight = '" . (float)$product_option_value['weight'] . 
						"', option_sort = '" . (int)$product_option['option_sort'] . 
						"', value_sort = '" . (int)$product_option_value['value_sort'] . "'");
				}
			}
		}
		
		if (isset($data['product_promotion'])) {
			foreach ($data['product_promotion'] as $product_promotion) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_promotion SET product_id = '" . (int)$product_id . 
					"', promotion_id = '" . (int)$product_promotion['promotion_id'] . 
					"', price = '" . (float)$product_promotion['price'] . 
					"', quantity = '" . (int)$product_promotion['quantity'] . 
					"', start_time = '" . strtotime($product_promotion['start_time']) . 
					"', end_time = '" . strtotime($product_promotion['end_time']) . "'");
			}
		}

		if (isset($data['product_wholesale'])) {
			foreach ($data['product_wholesale'] as $product_wholesale) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_wholesale SET product_id = '" . (int)$product_id . 
					"', min_num = '" . (int)$product_wholesale['min_num'] . 
					"', max_num = '" . (int)$product_wholesale['max_num'] . 
					"', price = '" . (float)$product_wholesale['price'] . "'");
			}
		}
		
		if (isset($data['product_related'])) {
			foreach ($data['product_related'] as $product_related) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . 
					"', related_product_id = '" . (int)$product_related['related_product_id'] . 
					"', price = '" . (float)$product_related['price'] . "'");
			}
		}

		if (isset($data['product_category'])) {
			foreach ($data['product_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . 
					"', category_id = '" . (int)$category_id . "'");
			}
		}
		
		$this->cache->delete('product');
	}
	
	public function editProduct($product_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "product SET category_id = '" . (int)$data['category_id'] . 
			"', manufacturer_id = '" . (int)$data['manufacturer_id'] . 
			"', `name` = '" . $this->db->escape($data['name']) . 
			"', ean = '" . $this->db->escape($data['ean']) . 
			"', model = '" . $this->db->escape($data['model']) . 
			"', image = '" .  $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . 
			"', price = '" . (float)$data['price'] . 
			"', weight = '" . (float)$data['weight'] . 
			"', quantity = '" . (int)$data['quantity'] . 
			"', minimum = '" . (int)$data['minimum'] . 
			"', processing_day = '" . (int)$data['processing_day'] . 
			"', layout_id = '" . (int)$data['layout_id'] . 
			"', sort_order = '" . (int)$data['sort_order'] . 
			"', status = '" . (int)$data['status'] . 
			"', date_modified = UNIX_TIMESTAMP() WHERE product_id = '". (int)$product_id . "'");
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
		foreach ($data['product_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '" . $this->db->escape($value['display_name']) . 
				"', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . 
				"', meta_description = '" . $this->db->escape($value['meta_description']) . 
				"', tag = '" . $this->db->escape($value['tag']) . 
				"', description = '" . $this->db->escape($value['description']) . "'");
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "'");
		if (isset($data['product_image'])) {
			foreach ($data['product_image'] as $product_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_image SET product_id = '" . (int)$product_id . 
					"', image = '" . $this->db->escape(html_entity_decode($product_image['image'], ENT_QUOTES, 'UTF-8')) . 
					"', sort_order = '" . (int)$product_image['sort_order'] . "'");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_attribute WHERE product_id = '" . (int)$product_id . "'");
		if (isset($data['product_attribute'])) {
			foreach ($data['product_attribute'] as $product_attribute) {
				foreach($product_attribute['product_attribute_value'] as $product_attribute_value){
					$this->db->query("INSERT INTO " . DB_PREFIX . "product_attribute SET product_id = '" . (int)$product_id . 
						"', attribute_id = '" . (int)$product_attribute['attribute_id'] . 
						"', attribute_value_id = '" . (int)$product_attribute_value['attribute_value_id'] . 
						"', attr_sort = '" . (int)$product_attribute['attr_sort'] . 
						"', value_sort = '" . (int)$product_attribute_value['value_sort'] .  "'");
				}
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_option WHERE product_id = '" . (int)$product_id . "'");
		if (isset($data['product_option'])) {
			foreach ($data['product_option'] as $product_option) {
				foreach($product_option['product_option_value'] as $product_option_value){
					$this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . 
						"', option_id = '" . (int)$product_option['option_id'] . 
						"', option_value_id = '" . (int)$product_option_value['option_value_id'] . 
						"', required = '" . (int)$product_option['required'] . 
						"', quantity = '" . (int)$product_option_value['quantity'] . 
						"', price = '" . (float)$product_option_value['price'] . 
						"', weight = '" . (float)$product_option_value['weight'] . 
						"', option_sort = '" . (int)$product_option['option_sort'] . 
						"', value_sort = '" . (int)$product_option_value['value_sort'] . "'");
				}
			}
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "product_promotion WHERE product_id = '" . (int)$product_id . "'");
		if (isset($data['product_promotion'])) {
			foreach ($data['product_promotion'] as $product_promotion) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_promotion SET product_id = '" . (int)$product_id . 
					"', promotion_id = '" . (int)$product_promotion['promotion_id'] . 
					"', price = '" . (float)$product_promotion['price'] . 
					"', quantity = '" . (int)$product_promotion['quantity'] . 
					"', start_time = '" . strtotime($product_promotion['start_time']) . 
					"', end_time = '" . strtotime($product_promotion['end_time']) . "'");
			}
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "product_wholesale WHERE product_id = '" . (int)$product_id . "'");
		if (isset($data['product_wholesale'])) {
			foreach ($data['product_wholesale'] as $product_wholesale) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_wholesale SET product_id = '" . (int)$product_id . 
					"', min_num = '" . (int)$product_wholesale['min_num'] . 
					"', max_num = '" . (int)$product_wholesale['max_num'] . 
					"', price = '" . (float)$product_wholesale['price'] . "'");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");
		if (isset($data['product_related'])) {
			foreach ($data['product_related'] as $product_related) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . 
					"', related_product_id = '" . (int)$product_related['related_product_id'] . 
					"', price = '" . (float)$product_related['price'] . "'");
			}
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
		if (isset($data['product_category'])) {
			foreach ($data['product_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . 
					"', category_id = '" . (int)$category_id . "'");
			}
		}
						
		$this->cache->delete('product');
	}
	
	public function getProduct($product_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p WHERE p.product_id = '" . (int)$product_id . "'");
		return $query->row;
	}
	
	public function getProductDescriptions($product_id) {
		$product_description_data = array();
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_description WHERE product_id = '" . (int)$product_id . "'");
		foreach ($query->rows as $result) {
			$product_description_data[$result['language_id']] = array(
				'display_name'     => $result['display_name'],
				'meta_keyword'     => $result['meta_keyword'],
				'meta_description' => $result['meta_description'],
				'tag'              => $result['tag'],
				'description'      => $result['description']
			);
		}
		return $product_description_data;
	}

	public function getProductAttributes($product_id) {
		$lang_id = (int)$this->config->get('config_language_id');
		//attribute
		$product_attribute_data = array();
		$query = $this->db->query("SELECT pa.attribute_id, pa.attr_sort, a.`name`, ad.display_name, " . 
			"ag.attribute_group_id, ag.`name` group_name, agd.display_name group_display_name FROM " . 
			DB_PREFIX . "product_attribute pa LEFT JOIN " . 
			DB_PREFIX . "attribute a ON pa.attribute_id = a.attribute_id LEFT JOIN " . 
			DB_PREFIX . "attribute_description ad ON a.attribute_id = ad.attribute_id AND ad.language_id = '" . $lang_id . "' LEFT JOIN " . 
			DB_PREFIX . "attribute_group ag ON a.attribute_group_id = ag.attribute_group_id LEFT JOIN " . 
			DB_PREFIX . "attribute_group_description agd ON ag.attribute_group_id = agd.attribute_group_id AND agd.language_id = '" . $lang_id . 
			"' WHERE pa.product_id = '" . (int)$product_id . "' GROUP BY pa.attribute_id ORDER BY pa.attr_sort");
		foreach ($query->rows as $product_attribute) {
			//product attribute value
			$query_value = $this->db->query("SELECT pa.attribute_value_id, pa.value_sort, av.`name`, av.image, avd.display_name FROM " . 
				DB_PREFIX . "product_attribute pa LEFT JOIN " . 
				DB_PREFIX . "attribute_value av ON pa.attribute_value_id = av.attribute_value_id LEFT JOIN " . 
				DB_PREFIX . "attribute_value_description avd ON av.attribute_value_id = avd.attribute_value_id AND avd.language_id = '" . $lang_id . 
				"' WHERE pa.product_id = '" . (int)$product_id . "' AND pa.attribute_id = '" . (int)$product_attribute['attribute_id'] . 
				"' ORDER BY pa.value_sort");
			$product_attribute_value_data = $query_value->rows;
			
			//attribute value
			$query_attr_value = $this->db->query("SELECT av.attribute_value_id, av.`name`, av.image, av.sort_order, avd.display_name FROM " . 
				DB_PREFIX . "attribute_value av LEFT JOIN " . 
				DB_PREFIX . "attribute_value_description avd ON av.attribute_value_id = avd.attribute_value_id AND avd.language_id = '" . $lang_id . 
				"' WHERE av.attribute_id = '" . (int)$product_attribute['attribute_id'] . 
				"' ORDER BY av.sort_order");
			$attribute_value_data = $query_attr_value->rows;
			
			$product_attribute_data[] = array(
				'attribute_id'                  => $product_attribute['attribute_id'],
				'attr_sort'                     => $product_attribute['attr_sort'],
				'name'                          => $product_attribute['name'],
				'display_name'                  => $product_attribute['display_name'],
				'attribute_group_id'            => $product_attribute['attribute_group_id'],
				'group_name'                    => $product_attribute['group_name'],
				'group_display_name'            => $product_attribute['group_display_name'],
				'product_attribute_value'       => $product_attribute_value_data,
				'attribute_value'               => $attribute_value_data
			);
		}
		
		return $product_attribute_data;
	}
	
	public function getProductOptions($product_id) {
		$lang_id = (int)$this->config->get('config_language_id');
		//option
		$product_option_data = array();
		$query = $this->db->query("SELECT po.option_id, po.required, po.option_sort, " . 
			"o.`type`, o.`name`, o.option_unit, o.min_size, o.max_size, od.display_name FROM " . 
			DB_PREFIX . "product_option po LEFT JOIN `" . 
			DB_PREFIX . "option` o ON po.option_id = o.option_id LEFT JOIN " . 
			DB_PREFIX . "option_description od ON o.option_id = od.option_id AND od.language_id = '" . $lang_id . 
			"' WHERE po.product_id = '" . (int)$product_id . "' GROUP BY po.option_id ORDER BY po.option_sort");
		foreach ($query->rows as $product_option) {
			//product option value
			$query_value = $this->db->query("SELECT po.option_value_id, po.quantity, po.price, po.weight, " . 
				"po.value_sort, ov.`name`, ov.image, ovd.display_name FROM " . 
				DB_PREFIX . "product_option po LEFT JOIN " . 
				DB_PREFIX . "option_value ov ON po.option_value_id = ov.option_value_id LEFT JOIN " . 
				DB_PREFIX . "option_value_description ovd ON ov.option_value_id = ovd.option_value_id AND ovd.language_id = '" . $lang_id . 
				"' WHERE po.product_id = '" . (int)$product_id . "' AND po.option_id = '" . (int)$product_option['option_id'] . 
				"' ORDER BY po.value_sort");
			$product_option_value_data = $query_value->rows;
			
			//option value
			$query_value = $this->db->query("SELECT ov.option_value_id, ov.`name`, ov.image, ov.sort_order, ovd.display_name FROM " . 
				DB_PREFIX . "option_value ov LEFT JOIN " . 
				DB_PREFIX . "option_value_description ovd ON ov.option_value_id = ovd.option_value_id AND ovd.language_id = '" . $lang_id . 
				"' WHERE ov.option_id = '" . (int)$product_option['option_id'] . 
				"' ORDER BY ov.sort_order");
			$option_value_data = $query_value->rows;
			
			$product_option_data[] = array(
				'option_id'            => $product_option['option_id'],
				'required'             => $product_option['required'],
				'option_sort'          => $product_option['option_sort'],
				'type'                 => $product_option['type'],
				'name'                 => $product_option['name'],
				'option_unit'          => $product_option['option_unit'],
				'min_size'             => $product_option['min_size'],
				'max_size'             => $product_option['max_size'],
				'display_name'         => $product_option['display_name'],
				'product_option_value' => $product_option_value_data,
				'option_value'         => $option_value_data
			);				
		}	
		return $product_option_data;
	}
	
	public function getProductImages($product_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "' ORDER BY sort_order");
		return $query->rows;
	}
	
	public function getProductPromotion($product_id) {
		$query = $this->db->query("SELECT a.product_id, a.promotion_id, a.price, a.quantity, " . 
			"FROM_UNIXTIME(a.start_time, '%Y-%m-%d') start_time, FROM_UNIXTIME(a.end_time, '%Y-%m-%d') end_time, " . 
			"b.`name`, b.image, FROM_UNIXTIME(b.start_time, '%Y-%m-%d') start_time_2, FROM_UNIXTIME(b.end_time, '%Y-%m-%d') end_time_2, b.`status`, c.display_name FROM " . 
			DB_PREFIX . "product_promotion a LEFT JOIN " . 
			DB_PREFIX . "promotion b on a.promotion_id = b.promotion_id LEFT JOIN " . 
			DB_PREFIX . "promotion_description c on b.promotion_id = c.promotion_id AND c.language_id = '" . 
			(int)$this->config->get('config_language_id') . "' WHERE a.product_id = '" . 
			(int)$product_id . "' ORDER BY b.`status` DESC, a.end_time DESC");
		return $query->rows;
	}
	
	public function getProductRelated($product_id) {
		$query = $this->db->query("SELECT pr.`product_id`, pr.`related_product_id`, pr.`price`, p.`name`, p.`price` price2, pd.`display_name` FROM " . 
			DB_PREFIX . "product_related pr LEFT JOIN " . 
			DB_PREFIX . "product p ON pr.`related_product_id` = p.`product_id` LEFT JOIN " . 
			DB_PREFIX . "product_description pd ON p.`product_id` = pd.`product_id` AND pd.`language_id` = '" . 
			(int)$this->config->get('config_language_id') . "' WHERE pr.`product_id` = '" . (int)$product_id . "'");
		return $query->rows;
	}
	
	public function getProductWholesale($product_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_wholesale WHERE product_id = '" . (int)$product_id . "' ORDER BY min_num");
		return $query->rows;
	}
		
	public function getProductCategories($product_id) {
		$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
		$product_category_data = array();
		foreach ($query->rows as $product_category) {
			$product_category_data[] = $product_category['category_id'];;				
		}
		return $product_category_data;
	}
	
	public function getProducts($data = array()) {
		$sql = "SELECT p.product_id, p.`name`, p.ean, p.model, p.image, p.price, p.quantity, p.sort_order, p.status, pd.display_name FROM " . 
			DB_PREFIX . "product p LEFT JOIN " . 
			DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id AND pd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "')";
		
		if (isset($data['filter_category_id'])) {
			$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id)";			
		}
				
		$sql_params = ""; 
		if (isset($data['filter_name'])) {
			$sql_params .= " AND LCASE(pd.display_name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
		}
		if (isset($data['filter_ean'])) {
			$sql_params .= " AND LCASE(p.ean) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_ean'])) . "%'";
		}
		if (isset($data['filter_model'])) {
			$sql_params .= " AND LCASE(p.model) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_model'])) . "%'";
		}
		if (isset($data['filter_price_min'])) {
			$sql_params .= " AND p.price >= '" . $this->db->escape($data['filter_price_min']) . "'";
		}
		if (isset($data['filter_price_max'])) {
			$sql_params .= " AND p.price <= '" . $this->db->escape($data['filter_price_max']) . "'";
		}
		if (isset($data['filter_quantity_min'])) {
			$sql_params .= " AND p.quantity >= '" . $this->db->escape($data['filter_quantity_min']) . "'";
		}
		if (isset($data['filter_quantity_max'])) {
			$sql_params .= " AND p.quantity <= '" . $this->db->escape($data['filter_quantity_max']) . "'";
		}
		if (isset($data['filter_status']) && $data['filter_status'] > -1) {
			$sql_params .= " AND p.status = '" . (int)$data['filter_status'] . "'";
		}
		if (isset($data['filter_category_id'])) {
			$sql_params .= " AND (p.category_id = '" . (int)$data['filter_category_id'] . 
				"' OR p2c.category_id = '" . (int)$data['filter_category_id'] . "')";
		}
		if($sql_params != ""){
			$sql_params = trim($sql_params);
			$sql .= " WHERE " . ltrim($sql_params, "AND");
		}
					
		$sort_data = array(
			'pd.display_name',
			'p.ean',
			'p.model',
			'p.price',
			'p.quantity',
			'p.status',
			'p.sort_order'
		);	
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY pd.display_name";	
		}
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}				
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}	
		
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function getTotalProducts($data = array()) {
		$sql = "SELECT COUNT(DISTINCT p.product_id) AS total FROM " . 
			DB_PREFIX . "product p LEFT JOIN " . 
			DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id AND pd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "')";
		
		if (isset($data['filter_category_id'])) {
			$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id)";			
		}
		 			
		$sql_params = ""; 
		if (isset($data['filter_name'])) {
			$sql_params .= " AND LCASE(pd.display_name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
		}
		if (isset($data['filter_ean'])) {
			$sql_params .= " AND LCASE(p.ean) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_ean'])) . "%'";
		}
		if (isset($data['filter_model'])) {
			$sql_params .= " AND LCASE(p.model) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_model'])) . "%'";
		}
		if (isset($data['filter_price_min'])) {
			$sql_params .= " AND p.price >= '" . $this->db->escape($data['filter_price_min']) . "'";
		}
		if (isset($data['filter_price_max'])) {
			$sql_params .= " AND p.price <= '" . $this->db->escape($data['filter_price_max']) . "'";
		}
		if (isset($data['filter_quantity_min'])) {
			$sql_params .= " AND p.quantity >= '" . $this->db->escape($data['filter_quantity_min']) . "'";
		}
		if (isset($data['filter_quantity_max'])) {
			$sql_params .= " AND p.quantity <= '" . $this->db->escape($data['filter_quantity_max']) . "'";
		}
		if (isset($data['filter_status']) && $data['filter_status'] > -1) {
			$sql_params .= " AND p.status = '" . (int)$data['filter_status'] . "'";
		}
		if (isset($data['filter_category_id'])) {
			$sql_params .= " AND (p.category_id = '" . (int)$data['filter_category_id'] . 
				"' OR p2c.category_id = '" . (int)$data['filter_category_id'] . "')";
		}
		if($sql_params != ""){
			$sql_params = trim($sql_params);
			$sql .= " WHERE " . ltrim($sql_params, "AND");
		}
		
		$query = $this->db->query($sql);
		return $query->row['total'];
	}
	
	public function getProductForSelect($product_id) {
		$query = $this->db->query("SELECT p.product_id, p.price, pd.display_name FROM " . 
			DB_PREFIX . "product p LEFT JOIN " . 
			DB_PREFIX . "product_description pd ON p.product_id = pd.product_id AND pd.language_id = '" . 
			(int)$this->config->get('config_language_id') . 
			"' WHERE p.product_id = '" . (int)$product_id . "' AND p.status = 1");
		return $query->rows;
	}
}
?>